package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import cn.taketoday.live.WebSocketMessage;
import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.WelcomeGuard;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.utils.JodaTimeUtils;

/**
 * @author TODAY 2021/5/17 23:20
 */
public class WelcomeGuardCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    final JSONObject jsonObject = context.getOriginal();
    final StringBuilder stringBuilder = new StringBuilder(32);
    if (context.contains(ShieldMessage.is_welcome)) {
      WelcomeGuard welcomeGuard = JSONObject.parseObject(jsonObject.getString("data"), WelcomeGuard.class);
      stringBuilder.append(JodaTimeUtils.getCurrentDateTimeString());
      switch (welcomeGuard.getGuard_level()) {
        case 3:
          stringBuilder.append(":欢迎舰长:");
          break;
        case 2:
          stringBuilder.append(":欢迎提督:");
          break;
        case 1:
          stringBuilder.append(":欢迎总督:");
          break;
      }
      stringBuilder.append(welcomeGuard.getUsername());
      stringBuilder.append(" 进入直播间");
      //控制台打印
      if (context.contains(ShieldMessage.is_cmd)) {
        System.out.println(stringBuilder.toString());
      }
      context.sendMessage(WebSocketMessage.of("welcomeGuard", welcomeGuard));
      if (GlobalData.logThread != null && !GlobalData.logThread.FLAG) {
        GlobalData.logString.add(stringBuilder.toString());
        synchronized(GlobalData.logThread) {
          GlobalData.logThread.notify();
        }
      }
      stringBuilder.delete(0, stringBuilder.length());
    }
    // LOGGER.debug("舰长大大进来直播间了:::" + message);
  }

}
